﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using System.IO;

namespace DriverHome.Web.Control.Upload.Excel
{
    /// <summary>
    /// UpLoadE 的摘要说明
    /// </summary>
    public class UpLoadE : IHttpHandler
    {
        public int Config_PhotoMaxWidth = Koumi.Tools.ConvertHelper.StrToInt(System.Configuration.ConfigurationManager.AppSettings["PhotoMaxWidth"].ToString(), 600);

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            string str = UploadFile();

            context.Response.Write(str);
        }

        #region 执行上传
        private string UploadFile()
        {
            string source = "{{\"Result\":\"{0}\",\"Index\":\"{1}\",\"FileName\":\"{2}\",\"Msg\":\"{3}\"}}";

            #region 读取参数
            int _index = Koumi.Tools.WebUtils.GetQueryInt("num", 0);
            string _key = Koumi.Tools.WebUtils.GetQueryString("key");
            string _type = Koumi.Tools.WebUtils.GetQueryString("type").ToLower();
            if (string.IsNullOrEmpty(_type))
                _type = "img";
            #endregion

            #region 读取上传文件
            if (HttpContext.Current.Request.Files.Count == 0)
            {
                return string.Format(source, "0", _index.ToString(), string.Empty, "请选择要上传的文件！");
            }

            HttpPostedFile _postFile = HttpContext.Current.Request.Files["upFile"];

            if (_postFile.ContentLength <= 0)
            {
                return string.Format(source, "0", _index.ToString(), string.Empty, "请选择要上传的文件！");
            }
            string _postFileName = _postFile.FileName;

            string _ext = Koumi.Tools.FileFactory.File.GetFileExt(_postFileName);

            #endregion

            #region 读取上传配置
            string _root = System.Configuration.ConfigurationManager.AppSettings["UploadRoot"].ToString();
            if (_root.IndexOf(@":\") == -1)
                _root = HttpContext.Current.Server.MapPath(_root);


            string _fileExt = string.Empty;

            //_fileExt = System.Configuration.ConfigurationManager.AppSettings.Get("ExcelExt");
            _fileExt = "txt|xls|xlsx";//todo

            #endregion

            #region 验证文件
            //扩展
            if (("|" + _fileExt + "|").ToLower().IndexOf("|" + _ext.ToLower() + "|") == -1)
            {
                return string.Format(source, "0", _index.ToString(), string.Empty, "文件类型错误，仅支持：" + _fileExt);
            }
            #endregion

            #region 保存文件
            string _fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString("000");
            string _savePath = HttpContext.Current.Server.MapPath("/Upload/Temp/");//todo

            //不存在路径时创建其路径
            if (!Directory.Exists(_savePath))
                Directory.CreateDirectory(_savePath);

            string _saveFile = Path.Combine(_savePath, _fileName + "." + _ext).Replace(@"\", "/");

            try
            {
                _postFile.SaveAs(_saveFile);
                _postFile = null;

            }
            catch
            {
                _postFile = null;
                return string.Format(source, "0", _index.ToString(), string.Empty, "文件上传失败！");
            }
            
            #endregion
            return string.Format(source, "1", _index.ToString(), _fileName + "." + _ext, "上传成功！");

        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}